/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.jennic.internal.temperature;

import java.util.HashMap;
import java.util.Map;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.osami.gateway.jennic.IUpdatableSense;
import org.osami.gateway.jennic.internal.JennicDevice;
import org.osami.gateway.sensor.SenseData;
import org.osami.gateway.storage.ISenseDataStorage;
import org.osgi.service.device.Device;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 26 mai 2010
 * Time: 18:12:40
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
public class TemperatureSense implements IUpdatableSense {

    @ServiceProperty(name = "sense.type",
                     value = JennicDevice.TEMPERATURE_KEY)
    private String type;

    @ServiceProperty(name = "device.id",
                     mandatory = true)
    private String deviceId;

    @Requires(id = "device",
              filter = "(service.id=<dynamically-set>)")
    private Device device;

    @Property(mandatory = true)
    private String topic;

    @Property(mandatory = true)
    private String name;

    private SenseData data;

    @Requires
    private ISenseDataStorage dataStore;

    @Requires
    private EventAdmin eventAdmin;

    public SenseData update(int newValue) {

        // Store SenseData
        data = new TemperatureSenseData(getName(), newValue);
        publish(data);

        return data;
    }

    public String getNodeId() {
        return deviceId;
    }

    public String getName() {
        return deviceId + "/" + name;
    }

    public SenseData getData() {
        return data;
    }

    public String getTopic() {
        return topic + "/" + name;
    }

    private void publish(SenseData senseData) {

        dataStore.store(senseData);

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(SenseData.DEFAULT_DATA_KEY, senseData);
        Event event = new Event(getTopic(), properties);
        eventAdmin.postEvent(event);
    }

}
